﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;

namespace IEWebSite.Administration
{
    public partial class CadreDidactice : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
        {
            if (e.Item is GridEditableItem && e.Item.IsInEditMode)
            {
                GridBinaryImageColumnEditor editor = ((GridEditableItem)e.Item).EditManager.GetColumnEditor("Upload") as GridBinaryImageColumnEditor;
                TableCell cell = (TableCell)editor.RadUploadControl.Parent;
                CustomValidator validator = new CustomValidator();
                validator.ErrorMessage = "Selectati un o poza pentru a fi incarcata!";
                validator.ClientValidationFunction = "validateRadUpload";
                validator.Display = ValidatorDisplay.Dynamic;
                cell.Controls.Add(validator);
            }
        }

        protected void RadGrid1_ItemUpdated(object source, Telerik.Web.UI.GridUpdatedEventArgs e)
        {
            GridEditableItem item = (GridEditableItem)e.Item;
            String id = item.GetDataKeyValue("IdPerosnalDidactic").ToString();
            //GridBinaryImageColumnEditor editor = ((GridEditableItem)e.Item).EditManager.GetColumnEditor("Upload") as GridBinaryImageColumnEditor;
           
            if (e.Exception != null)
            {
                e.KeepInEditMode = true;
                e.ExceptionHandled = true;
                SetMessage("Cadrul didactic cu ID " + id + " nu poate fi updatat. Motivul: " + e.Exception.Message);
            
            }
            else
            {
                SetMessage("Cadrul didactic cu ID " + id + " a fost modificat cu succes!");
            }

           
               
            
        }

        protected void RadGrid1_ItemInserted(object source, GridInsertedEventArgs e)
        {
            GridBinaryImageColumnEditor editor = ((GridEditableItem)e.Item).EditManager.GetColumnEditor("Upload") as GridBinaryImageColumnEditor;
            TableCell cell = (TableCell)editor.RadUploadControl.Parent;
            CustomValidator validator = new CustomValidator();
            validator.ErrorMessage = "Selectati un o poza pentru a fi incarcata!";
            validator.ClientValidationFunction = "validateRadUpload";
            validator.Display = ValidatorDisplay.Dynamic;
            cell.Controls.Add(validator);
            if (e.Exception != null)
            {
                e.ExceptionHandled = true;
                SetMessage("Nu poate fi inserat un cadru didactic. Motivul: " + e.Exception.Message);
            }
            else
            {
                SetMessage("Un cadru didactic nou a fost inserat!");
            }
        }

        protected void RadGrid1_ItemDeleted(object source, GridDeletedEventArgs e)
        {
            GridDataItem dataItem = (GridDataItem)e.Item;
            String id = dataItem.GetDataKeyValue("IdPerosnalDidactic").ToString();

            if (e.Exception != null)
            {
                e.ExceptionHandled = true;
                SetMessage("Cadul didactic cu ID: " + id + " nu poate fi sters. Motivul: " + e.Exception.Message);
            }
            else
            {
                SetMessage("Cadrul didactic cu ID " + id + " a fost sters!");
            }
        }

        private void SetMessage(string message)
        {
            gridMessage = message;
        }

        private string gridMessage = null;

        protected void RadGrid1_DataBound(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(gridMessage))
            {
                DisplayMessage(gridMessage);
            }
        }
        private void DisplayMessage(string text)
        {
            rdgProfesor.Controls.Add(new LiteralControl(string.Format("<span style='color:red'>{0}</span>", text)));
        }
    }
}
